package soft.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import soft.pojo.*;
import soft.pojo.Class;
import soft.service.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminControl {

    @Autowired
    private AdminService adminService;
    @Autowired
    private ClassService classService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;

    //登录
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String aId, String password,String permissions, ModelMap modelMap,HttpSession session){
        System.out.println(aId+password+permissions);
        if(Integer.parseInt(permissions)==3){
            Admin bean = adminService.login(aId,password);
            if(bean != null){
                //session保存
                //	可以跨页面传输，方便调用，暂时还没有用上
                session.setAttribute("loginAdmin",bean);
                session.setAttribute("aId",aId);
                return "seccess";
            }
            else{
                modelMap.addAttribute("msg","账号或密码错误");
                return "error";
            }
        }
        else if(Integer.parseInt(permissions)==2){
            Teacher bean = teacherService.login(aId,password);
            if(bean != null){
                //session保存
                //	可以跨页面传输，方便调用，暂时还没有用上
                session.setAttribute("loginTeacher",bean);
                session.setAttribute("aId",aId);
                return "teacherSeccess";
            }
            else{
                modelMap.addAttribute("msg","账号或密码错误");
                return "error";
            }
        }
        else{
            Student bean = studentService.login(aId,password);
            if(bean != null){
                //session保存
                //	可以跨页面传输，方便调用，暂时还没有用上
                session.setAttribute("loginStudent",bean);
                session.setAttribute("aId",aId);
                return "studentSeccess";
            }
            else{
                modelMap.addAttribute("msg","账号或密码错误");
                return "error";
            }
        }


    }
    //注册
    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public String register(String aId, String password, String cfmpassword, String permissions, Admin admin, Teacher teacher, Student student, ModelMap model,HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        admin.setaId(aId);
        admin.setPassword(password);
        admin.setPermissions(Integer.parseInt(permissions));
        admin.setRegTime(new Date());
        teacher.settId(aId);
        student.setsId(aId);
        System.out.println(admin);
        System.out.println(teacher);
        System.out.println(student);
        if(Integer.parseInt(permissions)==3){
            if(adminService.getById(admin.getaId())!=null) {
                model.addAttribute("msg", "账号已经存在");
                return "forward:/register.jsp";
            }
            if(!password.equals(cfmpassword)) {
                model.addAttribute("msg", "两次输入密码不一致");
                return "forward:/register.jsp";
            }
            //获取系统时间
            boolean flag = adminService.insert(admin);
            if(flag == true){
                model.addAttribute("msg", "操作成功，请登录");
                return "forward:/login.jsp";
            }
            else{
                response.sendRedirect(request.getContextPath()+"/register.jsp");
                return null;
            }
        }
        else if(Integer.parseInt(permissions)==2){
            if(teacherService.getById(teacher.gettId())!=null) {
                model.addAttribute("msg", "账号已经存在");
                return "forward:/register.jsp";
            }

            if(!password.equals(cfmpassword)) {
                model.addAttribute("msg", "两次输入密码不一致");
                return "forward:/register.jsp";
            }
            //获取系统时间
            teacher.setRegTime(new Date());
            System.out.println(teacher);
            boolean flag = teacherService.addTeacher(teacher);
            if(flag == true){
                model.addAttribute("msg", "操作成功，请登录");
                return "forward:/login.jsp";
            }
            else{
                response.sendRedirect(request.getContextPath()+"/register.jsp");
                return null;
            }
        }
        else{
            if(studentService.getById(student.getsId())!=null) {
                model.addAttribute("msg", "账号已经存在");
                return "forward:/register.jsp";
            }
            if(!password.equals(cfmpassword)) {
                model.addAttribute("msg", "两次输入密码不一致");
                return "forward:/register.jsp";
            }
            //获取系统时间
            student.setRegTime(new Date());
            boolean flag = studentService.insert(student);
            if(flag == true){
                model.addAttribute("msg", "操作成功，请登录");
                return "forward:/login.jsp";
            }
            else{
                response.sendRedirect(request.getContextPath()+"/register.jsp");
                return null;
            }

        }
    }
    //防报错
    @RequestMapping(value = "/welcome")
    public String deskTop( ModelMap modelMap,HttpSession session){

        return "error";
    }

    //查询个人信息
    @RequestMapping(value = "/getself")
    public String getSelf( ModelMap modelMap,HttpSession session){


        System.out.println(session.getAttribute("aId"));
        //得强制转化为String
       Admin bean = adminService.getById((String) session.getAttribute("aId"));
//          modelMap.addAttribute("adminmsg","bean");
          modelMap.addAttribute("adminmsg",bean);

            return "selectAdmin";
    }


    //查询到个人信息后更新
    @RequestMapping(value = "/getself1")
    public String getSelf1( ModelMap modelMap,HttpSession session){


        System.out.println(session.getAttribute("aId"));
        //得强制转化为String
        Admin bean = adminService.getById((String) session.getAttribute("aId"));
//          modelMap.addAttribute("adminmsg","bean");
        modelMap.addAttribute("adminmsg",bean);

        return "adminMsg";
    }
    //更新个人信息
    @RequestMapping(value = "/update")
    public String update(Admin bean, ModelMap modelMap,HttpSession session){
        System.out.println(bean);

       boolean flag =adminService.update(bean);
       if (flag == true){
           return "forward:/admin/getself";
       }else{
           return "adminMsg";
       }

    }

    //班级信息管理,先跳转到页面，在进行添加，保存
    @RequestMapping(value = "/gotoaddclass")
    public String gotoAddClass(){
        return "addClass";
    }

    @RequestMapping(value = "/addclass")
    public String addClass(Class bean, ModelMap model){
        System.out.println(bean);
        System.out.println(bean.getClassId());
        //判断班级ID是否已经存在
        if(classService.getById(bean.getClassId()) !=null ) {
            JOptionPane.showMessageDialog(null,"班级编号已经存在，请重新输入");
            return "addClass";
        }
        //判断班级名称是否已经存在
        if(classService.getById(bean.getClassName()) !=null ) {
            JOptionPane.showMessageDialog(null,"班级名称已经存在，请重新输入");
            return "addClass";
        }
        boolean flag = classService.insert(bean);
        if(flag == true){

            return "forward:/admin/allclass";
        }
        else{
            model.addAttribute("msg","操作有误，请重新操作");
            return "error";
        }
    }


    //查询所有班级信息
    @RequestMapping(value = "/allclass")
    public String allClass(ModelMap model,HttpSession session){
        List<Class> bean = classService.getList();
        model.addAttribute("allclass",bean);
   //     session.setAttribute("allclass",bean);
            return "findAllClass";
    }

    //删除班级信息
    @RequestMapping(value = "/deleteclass")
    public String deleteClass(String classId,ModelMap model,HttpSession session){
        System.out.println(classId);
        classService.delete(classId);
       // session.commit();
        List<Class> bean = classService.getList();
      //  session.setAttribute("allclass",bean);
        model.addAttribute("allclass",bean);
        return "findAllClass";
    }

    //班级信息管理,先跳转到页面，在进行添加，保存
    @RequestMapping(value = "/gotoupdateclass")
    public String gotoUpdateClass(String classId,ModelMap model){
        Class bean = classService.getById(classId);
        model.addAttribute("updateoneclass",bean);
        return "updateClass";
    }

    @RequestMapping(value = "/updateclass")
    public String updateClass(Class bean, ModelMap model){

        //判断班级名称是否已经存在
        if(classService.getById(bean.getClassName()) !=null ) {
            JOptionPane.showMessageDialog(null,"班级名称已经存在，请重新输入");
            return "updateClass";
        }
        boolean flag = classService.update(bean);
        if(flag == true){
//            返回一个方法，调用查找全部学生方法，返回查找全部页面
            return "forward:/admin/allclass";
        }
        else{
            model.addAttribute("msg","操作有误，请重新操作");
            return "error";
        }
    }


    //查询所有学生信息
    @RequestMapping(value = "/allstudent")
    public String allStudent(ModelMap model,HttpSession session){
        List<Student> bean = studentService.getList();
        model.addAttribute("allstudent",bean);
        //     session.setAttribute("allclass",bean);
        return "findAllStudent";
    }

    //删除学生信息
    @RequestMapping(value = "/deletestudent")
    public String deleteStudent(String sId,ModelMap model,HttpSession session){
        System.out.println(sId);
        studentService.deleteById(sId);
        // session.commit();
        List<Student> bean = studentService.getList();
        //  session.setAttribute("allclass",bean);
        model.addAttribute("allstudent",bean);
        return "findAllStudent";
    }

    //学生信息管理,先跳转到页面，在进行修改，保存
    @RequestMapping(value = "/gotoupdatestudent")
    public String gotoUpdateStudent(String sId,ModelMap model){
        System.out.println(sId);
        Student bean = studentService.getById(sId);
        model.addAttribute("updateonestudent",bean);
        return "updateStudent";
    }

    @RequestMapping(value = "/updatestudent")
    public String updateStudent(Student bean, ModelMap model){
        System.out.println(bean);
        boolean flag = studentService.update(bean);
        if(flag == true){
            return "forward:/admin/allstudent";
        }
        else{
            model.addAttribute("msg","操作有误，请重新操作");
            return "error";
        }
    }
    //学生信息管理,先跳转到页面，在进行添加，保存
    @RequestMapping(value = "/gotoaddstudent")
    public String gotoAddStudent(){
        return "addStudent";
    }

    @RequestMapping(value = "/addstudent")
    public String addStudent(Student bean, ModelMap model){
        System.out.println(bean.getsId());
        bean.setRegTime(new Date());
        boolean flag = studentService.insert(bean);
        if(flag == true){
            return "forward:/admin/allstudent";
        }
        else{
            model.addAttribute("msg","操作有误，请重新操作");
            return "error";
        }
    }



    //查询所有课程信息
    @RequestMapping(value = "/allcourse")
    public String allCourse(ModelMap model,HttpSession session){
        List<Course> bean = courseService.getList();
        model.addAttribute("allcourse",bean);
        //     session.setAttribute("allclass",bean);
        return "findAllCourse";
    }

    //删除课程信息
    @RequestMapping(value = "/deletecourse")
    public String deleteCourse(String courseId,ModelMap model,HttpSession session){
        System.out.println(courseId);
        courseService.delete(courseId);
        // session.commit();
        List<Course> bean = courseService.getList();
        //  session.setAttribute("allclass",bean);
        model.addAttribute("allcourse",bean);
        return "findAllCourse";
    }

    //课程信息管理,先跳转到页面，在进行修改，保存
    @RequestMapping(value = "/gotoupdatecourse")
    public String gotoUpdateCourse(String courseId,ModelMap model){
        Course bean = courseService.getById(courseId);
        model.addAttribute("updateonecourse",bean);
        return "updateCourse";
    }

    @RequestMapping(value = "/updatecourse")
    public String updateCourse(Course bean, ModelMap model){

        //判断课程名称是否已经存在
        if(courseService.getById(bean.getCourseName()) !=null ) {
            JOptionPane.showMessageDialog(null,"班级名称已经存在，请重新输入");
            return "updateCourse";
        }
        boolean flag = courseService.update(bean);
        if(flag == true){
            return "forward:/admin/allcourse";
        }
        else{
            model.addAttribute("msg","操作有误，请重新操作");
            return "error";
        }
    }

    //课程信息管理,先跳转到页面，在进行添加，保存
    @RequestMapping(value = "/gotoaddcourse")
    public String gotoAddCourse(){
        return "addCourse";
    }

    @RequestMapping(value = "/addcourse")
    public String addCourse(Course bean, ModelMap model){

        boolean flag = courseService.insert(bean);
        if(flag == true){
            return "forward:/admin/allcourse";
        }
        else{
            model.addAttribute("msg","操作有误，请重新操作");
            return "error";
        }
    }



    //查询教师
    @RequestMapping(value = "/findteacher")
    public String allTeacher(ModelMap model,HttpSession session){
        // session.commit();
        List<Teacher> bean = teacherService.getAll();
        //  session.setAttribute("allclass",bean);
        model.addAttribute("findteacher",bean);
        return "findTeacher";
    }
    //删除教师
    @RequestMapping(value = "/deleteteacher")
    public String deleteTeacher(String tId,ModelMap model,HttpSession session){
        System.out.println("tId:::"+tId);
        teacherService.deleteTeacher(tId);
        // session.commit();
        List<Teacher> bean = teacherService.getAll();
        //  session.setAttribute("allclass",bean);
        model.addAttribute("findteacher",bean);
        return "findTeacher";
    }

    //教师管理,先跳转到页面，在进行添加，保存
    @RequestMapping(value = "/gotoupdateteacher")
    public String goToUpdateGrade(String tId,ModelMap model){
        Teacher bean = teacherService.getById(tId);
        model.addAttribute("updateoneteacher",bean);
        return "updateTeacher";
    }
    //更新教师信息
    @RequestMapping(value = "/updateteacher")
    public String updateTeacher(Teacher bean, ModelMap model){
        teacherService.updateTeacher(bean);
        List<Teacher> bean1 = teacherService.getAll();
        //  session.setAttribute("allclass",bean);
        model.addAttribute("findteacher",bean1);
        return "forward:/admin/findteacher";
    }
    //跳转到添加页面
    @RequestMapping(value = "/gotoaddteacher")
    public String goToAddTeacher(){
        return "addTeacher";
    }

    //添加教师
    @RequestMapping(value = "/addteacher")
    public String addTeacher(Teacher bean, ModelMap model){
        bean.setRegTime(new Date());
        //判断教师编号是否已经存在
        if(teacherService.getById(bean.gettId()) !=null ) {
            JOptionPane.showMessageDialog(null,"教师编号已经存在，请重新输入");
            return "addTeacher";
        }
        teacherService.addTeacher(bean);
        List<Teacher> bean1 = teacherService.getAll();
        model.addAttribute("findteacher", bean1);

        return "forward:/admin/findteacher";
    }


}
